package com.wgo.bpot.client.servicerequest;

import java.net.URL;
import java.util.logging.Logger;

import com.wgo.bpot.common.transport.servicefacade.BpotServices;
import com.wgo.bpot.common.transport.servicefacade.RemoteServices;
import com.wgo.bpot.common.transport.servicefacade.ServiceFactory;
import com.wgo.bpot.common.transport.util.HessianServiceFactory;
import com.wgo.bpot.domain.common.AuthenticationHandler;

public abstract class AbstractHessianRemoteServicesFactory<S extends BpotServices> implements RemoteServicesFactory<S> {
    private AuthenticationHandler authenticationHandler;
    private RemoteServices remoteServices;
    private URL remoteUrl;
    private static final Logger log = Logger.getLogger(AbstractHessianRemoteServicesFactory.class.getSimpleName());
    
    public AbstractHessianRemoteServicesFactory(URL remoteUrl, AuthenticationHandler authenticationHandler){
    	this.remoteUrl = remoteUrl;
    	this.authenticationHandler = authenticationHandler;
    }
        
    public AuthenticationHandler getAuthenticationHandler(){
        return authenticationHandler;
    }

    public synchronized RemoteServices getRemoteServices() {
        if (null == remoteServices) {
            ServiceFactory serviceFactory = new HessianServiceFactory();
            log.info("Using the following remote URL: " + getRemoteUrl());
            remoteServices = serviceFactory.createService(remoteUrl, RemoteServices.class);
        }
        return remoteServices;
    }

    public URL getRemoteUrl() {
        return remoteUrl;
    }

    public void setRemoteUrl(URL newRemoteUrl) {
    	if (null == newRemoteUrl) {
    		throw new NullPointerException ("RemoteURL was null.");
    	}
        this.remoteUrl = newRemoteUrl;
    }
    
    public void setRemoteServices(RemoteServices newRemoteServices) {
    	if (null == newRemoteServices) {
    		throw new NullPointerException ("RemoteServices was null.");
    	}
        this.remoteServices = newRemoteServices;
    }

    public void setAuthenticationHandler(AuthenticationHandler newAuthenticationHandler) {
    	if (null == newAuthenticationHandler) {
    		throw new NullPointerException ("AuthenticationHandler was null.");
    	}
        this.authenticationHandler = newAuthenticationHandler;
    }
    
}
